<template>
  <div class="app-container">
    <!-- 卡片用户介绍 -->
    <el-card class="box-card">
      <h3>用户上架</h3>
      <el-form ref="users" :rules="rules" :model="users" label-width="150px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="users.userId" />
        </el-form-item>

        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="users.userName" />
        </el-form-item>

        <el-form-item label="用户性别" prop="userSex">
          <div class="dropdown">
          <template>

            <el-select v-model="users.userSex" placeholder="请选择">
              <el-option v-for="item in options" :key="item.label" :label="item.value" :value="item.value">
              </el-option>
            </el-select>
          </template>
        </div>
        </el-form-item>
        <el-form-item label="用户图片">
          <el-upload
          action="http://localhost:9600/common/upload" list-type="picture" :show-file-list="false"
            :on-success="uploadPicUrl" class="avatar-uploader" accept=".jpg,.jpeg,.png,.gif"
          >
            <img v-if="users.userImg" :src="users.userImg" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <el-image
      v-for="item in fileList"
      :key="item.uid"
      :src="item.url"
      fit="contain"
      style="max-width:200px; max-height:200px; margin-top:10px;"
    ></el-image>
        </el-form-item>






      </el-form>
    </el-card>

    <!-- 页面底部商家取消按钮 -->
    <div class="op-container">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handlePublish">上架</el-button>
    </div>
  </div>
</template>

<style>
.el-card {
  margin-bottom: 10px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}

.input-new-keyword {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.avatar-uploader .el-upload {
  width: 145px;
  height: 145px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import {saveUsers} from '@/api/user'
import { publishGoods, listCatAndBrand } from "@/api/goods/goods";
import { createStorage, uploadPath } from "@/api/system/storage";
import TinymceEditor from "@/components/TEditor.vue";
import { MessageBox } from "element-ui";
import { getToken } from "@/utils/auth";

export default {
  name: "GoodsCreate",
  components: { Editor: TinymceEditor },

  data() {
    return {
      options:[
        {
          label:"1",
          value:"男"
        },{
          label:"2",
          value:"女"
        }
      ],
      fileList:[],
      uploadPath, // 文件上传路径
      users: { userImg: "" }, // 用户信息
      // 验证规则
      rules: {
        userId: [
          { required: true, message: "用户编号不能为空", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
        ],
        userSex: [
          { required: true, message: "用户编号不能为空", trigger: "blur" },
        ],

      },
      // 富文本编辑器初始化
      editorInit: {
        language: 'zh_CN', // 汉化
        language_url: '/static/tinymce4.7.5langs/zh_CN.js', // 这里的地址指向 public 目录下的路径
        convert_urls: false,
        plugins: [
          "advlist anchor autolink autosave code codesample colorpicker colorpicker contextmenu directionality emoticons fullscreen hr image imagetools importcss insertdatetime link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace spellchecker tabfocus table template textcolor textpattern visualblocks visualchars wordcount",
        ],
        toolbar: [
          "searchreplace bold italic underline strikethrough alignleft aligncenter alignright outdent indent  blockquote undo redo removeformat subscript superscript code codesample",
          "hr bullist numlist link image charmap preview anchor pagebreak insertdatetime media table emoticons forecolor backcolor fullscreen",
        ],
        images_upload_handler: function (blobInfo, success, failure) {
          const formData = new FormData();
          formData.append("file", blobInfo.blob());
          createStorage(formData)
            .then((res) => {
              success(res.data.data.url);
            })
            .catch(() => {
              failure("上传失败，请重新上传");
            });
        },
      },
    };
  },
  computed: {
    // 上传文件请求头添加Token
    headers() {
      return {
        "Shopping-Admin-Token": getToken(),
      };
    },
  },
  created() {
    // 页面数据初始化
    this.init();
  },

  methods: {
    // 获取数据
    init: function () {
      /* listCatAndBrand().then((response) => {
        this.categoryList = response.data.data.categoryList;
        this.brandList = response.data.data.brandList;
      }); */
    },

    // 取消用户上架钩子
    handleCancel: function () {
      this.$router.push({ path: "/system/user" });
    },
    // 上架用户 钩子
    handlePublish: function () {
      // 构建数据
      const finalUsers = {

        userName: this.users.userName,
        userSex: this.users.userSex,
        userImg: this.users.userImg,
        userId: this.users.userId,
      };
      // 调用接口
      saveUsers(finalUsers)
        .then((response) => {
          console.log(response);
          this.$notify.success({
            title: "成功",
            message: "创建成功",
          });
          this.$router.push({ path: "/system/user" });
        })
        .catch((response) => {
          MessageBox.alert("业务错误：" + response.data.errmsg, "警告", {
            confirmButtonText: "确定",
            type: "error",
          });
        });
    },

    // 用户图片上传
    uploadPicUrl: function (response) {
      this.users.userImg = response.data;
    },
    // 上传文件个数限制
    uploadOverrun: function () {
      this.$message({
        type: "error",
        message: "上传文件个数超出限制!最多上传5张图片!",
      });
    }
  }
};
</script>
